<?php
/**
 * Notes:
 * Created by PhpStorm.
 * Author: TS
 * Date: 2019/10/13
 * Time: 23:01
 */

namespace app\server;


class TcpServer
{
    public function __construct()
    {
        //创建Server对象，监听 127.0.0.1:9501端口
        $serv = new \Swoole\Server("127.0.0.1", 9501);

//监听连接进入事件
        $serv->on('Connect', function ($serv, $fd) {
            echo "Client: Connect.\n";
        });

//监听数据接收事件
        $serv->on('Receive', function ($serv, $fd, $from_id, $data) {
            $serv->send($fd, "Server: ".$data);
        });

//监听连接关闭事件
        $serv->on('Close', function ($serv, $fd) {
            echo "Client: Close.\n";
        });

//启动服务器
        $serv->start();
    }

}
new TcpServer();